package com.symone.webcms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.Module;
import com.symone.webcms.model.ModuleField;
import com.symone.webcms.model.base.ResultMessage;
import com.symone.webcms.model.vo.DataContentVO;
import com.symone.webcms.service.ModuleFieldService;
import com.symone.webcms.service.ModuleService;
import com.xiaoleilu.hutool.json.JSONObject;
import com.xiaoleilu.hutool.json.JSONUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.plugins.Page;
import com.symone.webcms.service.DataContentService;
import com.symone.webcms.model.DataContent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *   @description : Content 管理
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2019-03-24
 */
@RestController
@Api(value="/content", description="Content 管理")
@RequestMapping("/api/content")
public class DataContentController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(DataContentController.class);

    @Autowired
    public DataContentService dataContentService;
    @Autowired
    public ModuleService moduleService;
    @Autowired
    public ModuleFieldService fieldService;

    /**
     * @description : 获取Content分页列表
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-24
     */
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ApiOperation(value="获取Content分页列表", notes="获取Content分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="groupId",value="分组ID"),
            @ApiImplicitParam(name="code",value="模型Code"),
            @ApiImplicitParam(name="fieldName",value="搜索字段"),
            @ApiImplicitParam(name="keyword",value="关键词"),
            @ApiImplicitParam(name="pageSize",value="页大小",required=true,defaultValue = "10"),
            @ApiImplicitParam(name="pageNum",value="页数",required=true,defaultValue = "1")
    })
    public ResultMessage page(Long groupId , String code , String fieldName , String keyword, Integer pageSize, Integer pageNum) {
        if(pageNum == null){
            pageNum = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }

        Page<DataContentVO> page = new Page<DataContentVO>(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        page = dataContentService.selectConditionPage(page,getSessionLanguage().getCode(),code, groupId, fieldName,keyword);
        List<DataContentVO> records = page.getRecords();
        List<Map<String,Object>> volist = new ArrayList<Map<String,Object>>();
        for(DataContentVO r : records){
            volist.add(r.getData());
        }
        return ResultMessage.success("获取Content的分页列表",volist,page.getTotal());
    }

    /**
     * @description : 获取所有Content记录
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-24
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value="获取所有Content信息", notes="获取所有Content信息")
    public ResultMessage list() {
        return ResultMessage.success("获取所有Content信息", dataContentService.findAll());
    }

    /**
     * @description : 通过id获取Content
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-24
     */
    @RequiresPermissions("data:content:get")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ApiOperation(value="获取Content信息", notes="获取Content信息")
    @ApiImplicitParam(name="id",value="ContentID",required=true)
    public ResultMessage get(Long id) {
        return ResultMessage.success("获取Content信息成功!", dataContentService.selectById(id));
    }

    /**
     * @description : 添加Content
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-24
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value="添加Content信息", notes="添加Content信息")
    @ApiImplicitParam(name = "content", value = "Content信息", required = true )
    public ResultMessage add(HttpServletRequest request){
        dataContentService.add(request,getSessionUser().getUserName());
        return ResultMessage.success("添加单页内容成功!");
    }

    /**
     * @description : 更新Content信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-24
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value="更新Content信息", notes="更新Content信息")
    @ApiImplicitParam(name = "content", value = "Content信息", required = true )
    public ResultMessage update(HttpServletRequest request) {
        dataContentService.update(request,getSessionUser().getUserName());
        return ResultMessage.success("Content信息更新成功!");
    }


    /**
     * @description : 更新Content信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-09
     */
    @RequestMapping(value = "/state", method = RequestMethod.POST)
    public ResultMessage update(DataContent content) {
        if(dataContentService.updateById(content)) {
            return ResultMessage.success("Content信息更新成功!");
        }else{
            return ResultMessage.fail("Content信息更新失败!");
        }
    }

    /**
     * @description : 通过id逻辑删除Content
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-24
     */
    @RequestMapping(value = "/removeFlag", method = RequestMethod.PUT)
    @ApiOperation(value="逻辑删除Content", notes="逻辑删除Content")
    @ApiImplicitParam(name = "id", value = "ContentId", required = true )
    public ResultMessage removeFlag(Long id){
        if(dataContentService.deleteFlagById(id)) {
            return ResultMessage.success("逻辑Content删除成功！");
        } else {
            return ResultMessage.fail("逻辑Content删除失败！");
        }
    }

    /**
     * @description : 通过id逻辑删除Content
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-24
     */
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    @ApiOperation(value="物理删除Content", notes="物理删除Content")
    @ApiImplicitParam(name = "id", value = "ContentId", required = true )
    public ResultMessage remove(long id){
        if(dataContentService.delete(id)) {
            return ResultMessage.success("物理删除Content成功！");
        } else {
            return ResultMessage.fail("物理删除Content失败！");
        }
    }

    /**
    * @description : 清除Content记录
    * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-24
    */
    @RequestMapping(value = "/clear", method = RequestMethod.DELETE)
    @ApiOperation(value="清除Content记录", notes="清除已被逻辑删除的Content记录")
    public ResultMessage clear(){
        int num = dataContentService.clear();
        if(num > 0) {
            return ResultMessage.success("已经清理了 "+ num +" 条记录！");
        } else {
            return ResultMessage.fail("没有Content记录被清除！");
        }
    }



}
